package com.sgb.kjwl.viewmodel.self.pick;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.ProListEntity;
import com.sgb.kjwl.model.entity.user.InvitationOfferEntity;
import com.sgb.kjwl.repository.SelfRepertory;
import com.sgb.kjwl.view.ui.activity.self.pick.InvitationOfferActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class InvitationOfferModel extends BaseViewModel {
    private InvitationOfferActivity activity;
    private SelfRepertory repertory;
    private int page = 1;
    private int size = 20;
    public int total;

    public InvitationOfferModel(InvitationOfferActivity activity, SelfRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
    }

    public void getData(String name){
        /*ArrayList<InvitationOfferEntity> userPickProjectEntities = new ArrayList<>();
        InvitationOfferEntity invitationOfferEntity = new InvitationOfferEntity();
        invitationOfferEntity.setCreateTime("2019-10-10");
        invitationOfferEntity.setInviteCompanyAttr("测试");
        invitationOfferEntity.setInviteCompanyBusinessAddress("陕西西安");
        invitationOfferEntity.setInviteCompanyName("中钧科技");
        invitationOfferEntity.setInviteCompanyRegisteredCapital("yiyiyiyiyiyiyiyiyiyiyi");
        invitationOfferEntity.setProId("114455");
        invitationOfferEntity.setProjName("跨海大桥");
        invitationOfferEntity.setProNo("666");
        invitationOfferEntity.setStatus("1");

        userPickProjectEntities.add(invitationOfferEntity);
        userPickProjectEntities.add(invitationOfferEntity);
        userPickProjectEntities.add(invitationOfferEntity);
        userPickProjectEntities.add(invitationOfferEntity);
        userPickProjectEntities.add(invitationOfferEntity);

        activity.showData(userPickProjectEntities);*/

        page =1;
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getInvitedOfferList(page, size, name).enqueue(new Callback<BaseEntity<ProListEntity<List<InvitationOfferEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ProListEntity<List<InvitationOfferEntity>>>> call, Response<BaseEntity<ProListEntity<List<InvitationOfferEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null){
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)){
                    total=response.body().getData().getTotalRow();
                    activity.showData(response.body().getData().getList());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ProListEntity<List<InvitationOfferEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getMoreData(String name){
        page++;
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getInvitedOfferList(page, size, name).enqueue(new Callback<BaseEntity<ProListEntity<List<InvitationOfferEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ProListEntity<List<InvitationOfferEntity>>>> call, Response<BaseEntity<ProListEntity<List<InvitationOfferEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null){
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)){
                    activity.showMoreData(response.body().getData().getList());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ProListEntity<List<InvitationOfferEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

}
